from rest_framework import serializers
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import status

from user.auth import UserAuth

from app_conf.models import AppInfo, CoverImage


class AppInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = AppInfo
        fields = "__all__"


class AppInfoViewSet(viewsets.ModelViewSet):
    authentication_classes = [UserAuth,]
    queryset = AppInfo.objects.all()
    serializer_class = AppInfoSerializer

    def list(self, request, *args, **kwargs):
        user_id = request.query_params.get("user_id")
        queryset = AppInfo.objects.filter(user=user_id).all()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    def create(self, request, *args, **kwargs):
        print("===========")
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)



# 封面照片相关
class CoverImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = CoverImage
        fields = "__all__"


class CoverImageViewSet(viewsets.ModelViewSet):
    authentication_classes = [UserAuth,]
    queryset = CoverImage.objects.all()
    serializer_class = CoverImageSerializer
